package com.admin.action;

import com.admin.service.DeveloperApplyService;
import com.data.util.CheckDataUtils;
import com.data.util.FinalStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 开发者申请
 *
 * @author:HingLo
 * @create 2017-09-13 15:10
 **/
@Controller
@RequestMapping("/DeveloperApplyAction/admin/")
public class DeveloperApplyAction {

    @Autowired
    private DeveloperApplyService developerApplyService;

    @RequestMapping("developerInput")
    public String inputDeveloperApplyAction(HttpServletRequest request, int type) {
        if (type == FinalStringUtils.COMPANY || type == FinalStringUtils.PERSON) {

            List<?> list = developerApplyService.findAllDeveloperApplyService(type);
            request.setAttribute("dataMap", list);
            if (type == FinalStringUtils.PERSON) {
                return "/WEB-INF/admin/apply/applyPerson";
            } else {
                return "/WEB-INF/admin/apply/applyCompany";
            }
        } else {
            return "404";
        }
    }

    /**
     * 图片查看
     *
     * @param id
     * @param response
     * @throws IOException
     */
    @RequestMapping("lookIconAction")
    public void lookIconAction(String id, HttpServletResponse response) throws IOException {
        //数据参数校验
        if (CheckDataUtils.stringUtilsID(id)) {
            byte[] inputStream = developerApplyService.lookIconService(id);
            response.setContentType("image/png");
            OutputStream stream = response.getOutputStream();
            stream.write(inputStream);
            stream.flush();
            stream.close();
        }


    }

    @RequestMapping("passApply")
    @ResponseBody
    public String passApplyAction(String id, int type) {
        if (type == FinalStringUtils.PERSON || type == FinalStringUtils.COMPANY) {
            boolean bool = developerApplyService.passApplyService(type, id);
            return String.valueOf(bool);
        }


        return String.valueOf(false);
    }

    @ResponseBody
    @PostMapping("refuseApply")
    public String refuseApplyAction(String content, String id, String email) {
        /**
         * 校验参数的有效性质
         */
        if (CheckDataUtils.stringUtils(content) && CheckDataUtils.stringUtilsID(id) && CheckDataUtils.checkEmail(email)) {
            boolean bool = developerApplyService.refuseApplyService(id, content, email);
            return String.valueOf(bool);
        }

        return String.valueOf(false);
    }

    /**
     * 查看拒绝原因的列表
     *
     * @param request
     * @return
     */
    @RequestMapping("applyRefuse")
    public String applyRefuseAction(HttpServletRequest request) {
        List<?> list = developerApplyService.applyRefuseService();
        request.setAttribute("dataMap", list);
        return "/WEB-INF/admin/apply/applyRefuse";
    }

    /**
     * 根据id来删除拒绝账号申请的原因列表
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteApply")
    @ResponseBody
    public String deleteApplyAction(String id) {
        return String.valueOf(CheckDataUtils.stringUtilsID(id) && developerApplyService.deleteApplyService(id));
    }

}
